﻿using System;
using System.Runtime.InteropServices;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Output;
using ESRI.ArcGIS.Controls;
using System.Collections.Generic;
using System.Drawing;
using System.IO;

namespace PlanningGIS.ArcGIS
{
    /// <summary>
    /// 概述：输出帮助类<br />
    /// </summary>
    [ComVisible(true)]
    public static class OutputHelper
    {
        /// <summary>
        /// 根据格式获取输出
        /// </summary>
        /// <param name="format"></param>
        /// <returns></returns>
        public static IExport GetExportByFormat(string format)
        {
            IExport export = null;
            switch (format.ToUpper())
            {
                case "PNG":
                    export = new ExportPNG() as IExport;
                    break;
                case "PNG8":
                    export = new ExportPNG() as IExport;
                    (export as IExportImage).ImageType = esriExportImageType.esriExportImageTypeIndexed;
                    //(export as IExportImage).ImageType = esriExportImageType.esriExportImageTypeTrueColor;
                    //export = new ExportJPEG() as IExport;
                    //(export as IExportJPEG).Quality = 100;
                    break;
                case "PNG24":
                    export = new ExportPNG() as IExport;
                    (export as IExportImage).ImageType = esriExportImageType.esriExportImageTypeTrueColor;
                    break;
                case "JPG":
                    export = new ExportJPEG() as IExport;
                    (export as IExportJPEG).Quality = 100;
                    break;
                case "PDF":
                    export = new ExportPDF() as IExport;
                    break;
                case "BMP":
                    export = new ExportBMP() as IExport;
                    break;
                case "GIF":
                    export = new ExportGIF() as IExport;
                    break;
                case "SVG":
                    export = new ExportSVG() as IExport;
                    break;
                default:
                    export = new ExportPNG() as IExport;
                    break;
            }
            return export;
        }

        /// <summary>
        /// 执行导出操作
        /// </summary>
        /// <param name="activeView">要导出的地图视图，MapControl.ActiveView  或 PageLayout.ActiveView</param>
        /// <param name="fullname">导出的全路径</param>
        /// <param name="visibleBounds">地图的可见范围</param>
        /// <param name="pixelBounds">导出的范围</param>
        /// <param name="format">要导出的格式</param>
        /// <param name="dpi">像素，整数</param>
        /// <param name="rgb">地图控件的背景色，MapControl.BackColor</param>
        /// <param name="transparent">透明，bool值</param>
        public static void Output(IActiveView activeView, string fullname, IEnvelope visibleBounds, tagRECT pixelBounds, string format, string dpi, int rgb = 0, string transparent = "true")
        {
            FileInfo fi = new FileInfo(fullname);
            if (!Directory.Exists(fi.DirectoryName))
                Directory.CreateDirectory(fi.DirectoryName);
            IExport export = OutputHelper.GetExportByFormat(format);
            export.ExportFileName = fullname;
            export.PixelBounds = new ESRI.ArcGIS.Geometry.Envelope()
            {
                XMax = pixelBounds.right,
                XMin = pixelBounds.left,
                YMax = pixelBounds.bottom,
                YMin = pixelBounds.top
            } as ESRI.ArcGIS.Geometry.IEnvelope;
            if (!string.IsNullOrEmpty(transparent) && Convert.ToBoolean(transparent))
            {
                if (export is IExportPNG)
                {
                    (export as IExportPNG).TransparentColor.RGB = rgb;
                    (export as IExportPNG).TransparentColor.Transparency = 0;
                    (export as IExportPNG).TransparentColor.NullColor = false;
                }
                else if (export is IExportGIF)
                {
                    (export as IExportGIF).TransparentColor.RGB = rgb;
                    (export as IExportGIF).TransparentColor.Transparency = 0;
                    (export as IExportGIF).TransparentColor.NullColor = false;
                }
            }
            export.Resolution = string.IsNullOrEmpty(dpi) ? 96 : Convert.ToInt32(dpi);
            Int32 hDC = export.StartExporting();
            activeView.Output(hDC, Convert.ToInt32(export.Resolution), ref pixelBounds, visibleBounds, null);
            export.FinishExporting();
            export.Cleanup();
        }

        /// <summary>
        /// 输出。
        /// </summary>
        /// <param name="export"></param>
        /// <param name="activeView"></param>
        /// <param name="pixelBounds"></param>
        /// <param name="visibleBounds"></param>
        public static void Output(IExport export, IActiveView activeView, ref tagRECT pixelBounds, IEnvelope visibleBounds)
        {
            Int32 hDC = export.StartExporting();
            activeView.Output(hDC, Convert.ToInt32(export.Resolution), ref pixelBounds, visibleBounds, null);
            export.FinishExporting();
            export.Cleanup();
        }

        /// <summary>
        /// 将地图输出到缓存里
        /// </summary>
        /// <param name="activeView"></param>
        /// <param name="pixelBounds"></param>
        /// <param name="visibleBounds"></param>
        /// <returns></returns>
        public static Image OutputImage(IActiveView activeView, ref tagRECT pixelBounds, IEnvelope visibleBounds)
        {
            Image img = new Bitmap(pixelBounds.right - pixelBounds.left, pixelBounds.bottom - pixelBounds.top);
            Graphics g = Graphics.FromImage(img);
            g.Clear(Color.Transparent);
            activeView.Output(g.GetHdc().ToInt32(), 96, ref pixelBounds, visibleBounds, null);
            g.ReleaseHdc();
            return img;
        }

        /// <summary>
        /// 获取布局中的所有地图框架对象IMapFrame
        /// </summary>
        /// <param name="pageLayout"></param>
        /// <returns></returns>
        public static List<IMapFrame> GetFrame(IPageLayoutControl3 pageLayout)
        {
            IGraphicsContainer graphicsContainer = pageLayout.GraphicsContainer;
            graphicsContainer.Reset();
            IElement element = null;
            List<IMapFrame> frames = new List<IMapFrame>();
            while ((element = graphicsContainer.Next()) != null)
            {
                if (element is IMapFrame)
                {
                    frames.Add(element as IMapFrame);
                }
            }
            return frames;
        }

        

    }
}
